<?php
require_once(APP_PATH . '/Common/user.php');

/**
 * 返回用户中心模型实例
 */
function UserCenterModel() {
	return D('Passport/UserCenter');
}


/**
 * [showRegError description]
 * @param  integer $code [description]
 * @return [type]        [description]
 */
function showRegError($code = 0) {
	switch ($code) {
            case -11:
                $error = '用户名长度必须在4-32个字符以内！';
                break;
            case -2:
                $error = '用户名被禁止注册！';
                break;
            case -12:
                $error = '用户名被占用！';
                break;
            case -41:
                $error = '密码长度必须在6-30个字符之间！';
                break;
            case -5:
                $error = '邮箱格式不正确！';
                break;
            case -6:
                $error = '邮箱长度必须在4-32个字符之间！';
                break;
            case -7:
                $error = '邮箱被禁止注册！';
                break;
            case -8:
                $error = '邮箱被占用！';
                break;
            case -9:
                $error = '手机格式不正确！';
                break;
            case -10:
                $error = '手机被禁止注册！';
                break;
            case -111:
                $error = '手机号被占用！';
                break;
            case -20:
                $error = '用户名只能由数字、字母和"_"组成！';
                break;
            case -30:
                $error = '昵称被占用！';
                break;
            case -31:
                $error = '昵称被禁止注册！';
                break;
            case -32:
                $error = '昵称只能由数字、字母、汉字和"_"组成！';
                break;
            case -33:
                $error = '昵称不能少于四个字！';
                break;
            default:
                $error = '未知错误24';
        }
        return $error;
}

function toFormatTree($list,$pid = 0,$level=0 , $title = 'name' ,$id="id" , $parent = 'parent') {
    static $formatTree = array();
    foreach($list as $key=>$val){
        $tmp_str = str_repeat("&nbsp;",$level*2);
        // $tmp_str.= "└─";
        $val['level']      = $level;
        $val['title_show'] = $val[$title];
        // $val['title_show'] = $level == 0 ? $val[$title]."&nbsp;":$tmp_str.$val[$title]."&nbsp;";
        $val['child']      = $val[$parent];
        if($val[$parent] ==  $pid) {
            array_push($formatTree,$val);
            toFormatTree($list,$val[$id],$level+1,$title ,$id, $parent); //进行下一层递归
        }
    }
    return $formatTree;
}

function listToTree($list , $pk = 'id' , $parent="pid" , $child = '_child' , $op = "child") {
    $reference = array();
    $tree      = array();
    if(is_array($list)) {
        foreach ($list as $key => $value) {
            //$reference[$value['id']] =& $value;  X
            $reference[$value['id']] =& $list[$key];
        }
        foreach ($list as $index => $v) {
            if($v[$parent] == 0) {
                $tree[] =& $list[$index];
            }else{
                if(isset($reference[$v[$parent]])) {
                    $myParent         =& $reference[$v[$parent]];
                    $myParent[$child][] =& $list[$index];
                }
            }
        }    
    }

    foreach ($tree as $key => $value) {
        if(isset($value[$child])) {
            $childs = $value[$child];
            $tree[$key][$op] = $childs;
            unset($tree[$key][$child]);
        }
    }
    return $tree;
    
}

function p($value , $exit = true) {
    echo "<pre>";
    print_r($value);
    if($exit) {
        exit;
    }
    
}